package com.jrx.anytxn.transaction.service.impl;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.entity.PrProductTable;
import com.jrx.anytxn.param.service.product.IProductTableService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.AmLoanMainInfo;
import com.jrx.anytxn.transaction.entity.TlPaymentAllocationLog;
import com.jrx.anytxn.transaction.entity.TlServiceFeeLog;
import com.jrx.anytxn.transaction.entity.TlThirdPartyPayment;
import com.jrx.anytxn.transaction.mapper.ext.ExtAmLoanMainInfoMapper;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlPaymentAllocationLogMapper;
import com.jrx.anytxn.transaction.service.IServiceFeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @program: anytxn-cf-parent
 * @description:
 * @author: xiaoyu.jing
 * @date: 2021-03-15 11:36
 **/
@Service
public class ServiceFeeServiceImpl implements IServiceFeeService {

    @Autowired
    private ISegmentService segmentService;
    @Resource
    private ExtAmLoanMainInfoMapper extLoanMainInfoMapper;
    @Resource
    private ExtTlPaymentAllocationLogMapper extTlPaymentAllocationLogMapper;
    @Autowired
    private IProductTableService productTableService;

    /**
     * 处理技术服务费
     * @param partyPayment
     * @return
     */
    @Override
    public TlServiceFeeLog dealServiceFee(TlThirdPartyPayment partyPayment) throws Exception {
        TlServiceFeeLog log = new TlServiceFeeLog();
        log.setId(segmentService.getId(TransBizConstant.TL_SERVICE_FEE_LOG_ID));
        String loanId = partyPayment.getLoanId();
        String customerId = partyPayment.getCustomerId();
        String tenantId = partyPayment.getTenantId();
        log.setTenantId(tenantId);
        log.setCustomerId(customerId);
        log.setLoanId(loanId);
        AmLoanMainInfo loanMainInfo = extLoanMainInfoMapper.selectByLoanIdAndCusId(loanId, customerId, tenantId);
        BigDecimal yearRate = loanMainInfo.getInterestRate();
        log.setLoanRate(yearRate);
        log.setContractNumber(loanMainInfo.getContractNumber());
        log.setPaySeqNo(partyPayment.getSeqNo());
        log.setTradeDate(partyPayment.getBusinessDate());
        log.setTradeMonth(DateUtils.format(partyPayment.getBusinessDate(),DateUtils.YEAR_MONTH_PATTERN));
        log.setChannel(partyPayment.getChannel());
        List<TlPaymentAllocationLog> logList = extTlPaymentAllocationLogMapper.selectBySeqNoAndTenantId(partyPayment.getSeqNo(),tenantId);
        BigDecimal intrAmt = logList.stream().filter(paymentAllocationLog -> TransBizConstant.ACCOUNT_TYPE_04.equals(paymentAllocationLog.getConnectAccountType()))
                .map(TlPaymentAllocationLog :: getPostingAmount).reduce(BigDecimal.ZERO,BigDecimal::add);
        if(intrAmt.compareTo(BigDecimal.ZERO) == 0){
            return null;
        }

        Set<Integer> billingSet = logList.stream().filter(paymentAllocationLog -> TransBizConstant.ACCOUNT_TYPE_04.equals(paymentAllocationLog.getConnectAccountType()))
                .map(TlPaymentAllocationLog::getBillingTenor).collect(Collectors.toSet());
        if(billingSet.size()>1){
            log.setBillingTenor(0);
        }else{
            log.setBillingTenor(billingSet.iterator().next());
        }

        log.setIntrAmt(intrAmt);
        BigDecimal fixedDebitAmt = intrAmt.multiply(BigDecimal.valueOf(0.01)).multiply(yearRate).setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal floatDebitAmt = intrAmt.multiply(BigDecimal.valueOf(0.02)).multiply(yearRate).setScale(2, BigDecimal.ROUND_HALF_UP);
        log.setFixedDebitAmt(fixedDebitAmt);
        log.setFloatDebitAmt(floatDebitAmt);
        log.setTradeFlag(this.transTradeFlag(partyPayment.getPaymentType()));

        PrProductTable prProductTable = productTableService.findProByProIdAndOrgId(loanMainInfo.getProductId(),loanMainInfo.getOrganizationId(),loanMainInfo.getTenantId());
        log.setProductType(prProductTable.getTableType());
        log.setProductId(prProductTable.getProductId());
        return log;
    }

    private String transTradeFlag(String paymentType){
        if(TransBizConstant.REPAYMENT_TYPE_PRETERMINTION.equals(paymentType)){
            return TransBizConstant.TRANSFER_FLAG_1;
        }else if(TransBizConstant.REPAYMENT_TYPE_OVERDUE.equals(paymentType)){
            return TransBizConstant.TRANSFER_FLAG_2;
        }else if(TransBizConstant.REPAYMENT_TYPE_PAY.equals(paymentType)){
            return TransBizConstant.TRANSFER_FLAG_0;
        }
        return null;
    }


}
